package android.demo.activity.dialog;

import android.app.Dialog;
import android.content.Context;
import android.demo.Constants.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyDialog extends Dialog implements View.OnClickListener {
	
	private final String TAG = Tag.DIALOG;
	private Context context;
	private MyDialogListener listener;
	
	public MyDialog(Context context, MyDialogListener listener) {
		super(context);
		this.context = context;
		this.listener = listener;
	}
	
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, this + "................onCreate");
        createView(context);
    }
    
    protected void onStart() {
		super.onStart();
		Log.i(TAG, this + " ................onStart");
	}
    
    @Override
    protected void onStop() {
    	super.onStop();
    	Log.i(TAG, this + " ................onStop");
    }
    
    @Override
    public Bundle onSaveInstanceState() {
    	Log.i(TAG, this + " ................onSave");
    	return super.onSaveInstanceState();
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	Log.i(TAG, this + " ................onRestore");
    }
    
    private void createView(Context context) {
    	LinearLayout rootLayout = new LinearLayout(context);
		rootLayout.setOrientation(LinearLayout.VERTICAL);
		
		TextView tv = new TextView(context);
		tv.setId(id.tvName);
		tv.setText("Enter Name:");
		rootLayout.addView(tv);
		
		EditText et = new EditText(context);
		et.setId(id.etName);
		et.setTextSize(18);
		rootLayout.addView(et);
		
    	LinearLayout buttonLayout = new LinearLayout(context);
    	buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
    	buttonLayout.setGravity(Gravity.CENTER_HORIZONTAL);
    	
    	Button btnOk = new Button(context);
    	btnOk.setId(id.btnOK);
    	btnOk.setText("OK");
    	buttonLayout.addView(btnOk);
    	
    	Button btnCancel = new Button(context);
    	btnCancel.setId(id.btnCancel);
    	btnCancel.setText("Cancel");
    	buttonLayout.addView(btnCancel);
    	
    	rootLayout.addView(buttonLayout);
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case id.btnOK:
				listener.onOkClick("测试回调");
				dismiss();
				break;
			case id.btnCancel:
				cancel();
				break;
		}
	}
	
	private static final class id {
		public static final int tvName = 11101;
		public static final int etName = 11102;
		public static final int btnOK = 11103;
		public static final int btnCancel = 11104;
	}
}

interface MyDialogListener {
	public void onOkClick(String name);
	public void onCancelClick();
}
